package com.ruoyi.project.emmanuel.memorial.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.ToolUtils;
import com.ruoyi.common.utils.file.FileUtils;
import com.ruoyi.common.utils.security.ShiroUtils;
import com.ruoyi.common.utils.text.Convert;
import com.ruoyi.framework.aspectj.lang.enums.AttachmentType;
import com.ruoyi.project.emmanuel.cloud.domain.BusinessFileLink;
import com.ruoyi.project.emmanuel.cloud.mapper.BusinessFileLinkMapper;
import com.ruoyi.project.emmanuel.cloud.service.IBusinessFileLinkService;
import com.ruoyi.project.emmanuel.memorial.domain.TimeLineS;
import com.ruoyi.project.emmanuel.memorial.mapper.TimeLineSMapper;
import com.ruoyi.project.emmanuel.memorial.service.ITimeLineSService;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * 时间轴Service业务层处理
 *
 * @author 一粒麦子
 * @date 2023-02-25
 */
@Service
public class TimeLineSServiceImpl extends ServiceImpl<TimeLineSMapper, TimeLineS> implements ITimeLineSService {

    @Autowired
    private TimeLineSMapper timeLineSMapper;
    @Autowired
    private IBusinessFileLinkService businessFileLinkService;
    @Autowired
    private BusinessFileLinkMapper businessFileLinkMapper;

    /**
     * 查询时间轴
     *
     * @param id 时间轴主键
     * @return 时间轴
     */
    @Override
    public TimeLineS selectTimeLineSById(Long id) {
        TimeLineS timeLineS = timeLineSMapper.selectById(id);
        // 获取附件
        BusinessFileLink businessFileLink = new BusinessFileLink();
        businessFileLink.setType(AttachmentType.TIME_ATTACHMENT.getType());
        businessFileLink.setBusinessId(timeLineS.getId());
        List<Map<String,Object>> mapList = businessFileLinkMapper.selectMapList(businessFileLink);
        mapList.forEach(m->{
            Object size = m.get("size");
            if (size != null){
                m.put("attachmentSize", FileUtils.formatBytes(Long.parseLong(String.valueOf(m.get("size")))));
            }
        });
        timeLineS.setAttachmentList(mapList);
        return timeLineS;
    }

    /**
     * 查询时间轴列表
     *
     * @param timeLineS 时间轴
     * @return 时间轴
     */
    @Override
    public List<TimeLineS> selectTimeLineSList(TimeLineS timeLineS) {
        QueryWrapper<TimeLineS> wq = new QueryWrapper<>();
        wq.lambda().eq(TimeLineS::getMId, timeLineS.getMId()).orderByDesc(TimeLineS::getCreateTime);
        return timeLineSMapper.selectList(wq);
    }

    /**
     * 新增时间轴
     *
     * @param timeLineS 时间轴
     * @return 结果
     */
    @Override
    public int insertTimeLineS(TimeLineS timeLineS) {
        timeLineS.setCreateTime(DateUtils.getNowDate());
        timeLineS.setCreateBy(ShiroUtils.getLoginName());
        int insert = timeLineSMapper.insert(timeLineS);
        // 新增附件
        List<Map<String,Object>> attachmentList = timeLineS.getAttachmentList();
        List<BusinessFileLink> businessFileLinkList = new ArrayList<>();
        if (CollectionUtils.isNotEmpty(attachmentList)){
            attachmentList.forEach(k->{
                BusinessFileLink businessFileLink = new BusinessFileLink();
                businessFileLink.setBusinessId(timeLineS.getId());
                businessFileLink.setAttachmentId(Long.parseLong(String.valueOf(k.get("attachmentId"))));
                businessFileLink.setType(AttachmentType.TIME_ATTACHMENT.getType());
                businessFileLinkList.add(businessFileLink);
            });
            businessFileLinkService.saveBatch(businessFileLinkList);
        }
        return insert;
    }

    /**
     * 修改时间轴
     *
     * @param timeLineS 时间轴
     * @return 结果
     */
    @Override
    public int updateTimeLineS(TimeLineS timeLineS) {
        timeLineS.setUpdateTime(DateUtils.getNowDate());
        timeLineS.setUpdateBy(ShiroUtils.getLoginName());

        int i = timeLineSMapper.updateById(timeLineS);

        // 删除附件
        HashMap<String, Object> delMap = new HashMap<>();
        delMap.put("business_id",timeLineS.getId());
        businessFileLinkMapper.deleteByMap(delMap);

        if (ToolUtils.isNotEmpty(timeLineS.getAttachmentList())){
            // 添加附件
            List<BusinessFileLink> businessFileLinkList = new ArrayList<>();
            timeLineS.getAttachmentList().forEach(k->{
                BusinessFileLink businessFileLink = new BusinessFileLink();
                businessFileLink.setBusinessId(timeLineS.getId());
                businessFileLink.setAttachmentId(Long.parseLong(String.valueOf(k.get("attachmentId"))));
                businessFileLink.setType(AttachmentType.TIME_ATTACHMENT.getType());
                businessFileLinkList.add(businessFileLink);
            });
            businessFileLinkService.saveBatch(businessFileLinkList);
        }
        return i;
    }

    /**
     * 批量删除时间轴
     *
     * @param ids 需要删除的时间轴主键
     * @return 结果
     */
    @Override
    public int deleteTimeLineSByIds(String ids) {
        return timeLineSMapper.deleteBatchIds(Arrays.asList(Convert.toStrArray(ids)));
    }

    /**
     * 获取时间轴详情
     *
     * @return
     */
    @Override
    public TimeLineS detail(Long id) {
        TimeLineS timeLineS = timeLineSMapper.selectById(id);
        // 获取附件
        BusinessFileLink businessFileLink = new BusinessFileLink();
        businessFileLink.setType(AttachmentType.TIME_ATTACHMENT.getType());
        businessFileLink.setBusinessId(timeLineS.getId());
        List<Map<String,Object>> mapList = businessFileLinkMapper.selectMapList(businessFileLink);
        mapList.forEach(m->{
            Object size = m.get("size");
            if (size != null){
                m.put("attachmentSize", FileUtils.formatBytes(Long.parseLong(String.valueOf(m.get("size")))));
            }
        });
        timeLineS.setAttachmentList(mapList);
        return timeLineS;
    }
}
